
const i18n = new VueI18n({
    locale: 'zh', // 设置默认
    messages
});


var em = new Vue({
    el: main,
    i18n,
    data: {
        //站点信息
        siteOptionValue:null,
        uploadtab:false,
        uploadfilename:'',
        siteValue:null,
        selectedsiteValue:null,
        echarttype:'duibi',
        echarttypeoptions:[
            {value:'duibi',label:'对比分析'},
            {value:'yancheng',label:'沿程分析'}
        ],

        areaQuery: '',
        fixedQuery: '',
        tableData: null, // 表格数据
        //数据展示
        siteOption: [{
            value: 'segment 1',
        }],
        dateValue: null,
        siteTypeOption: null,
        siteTypeOptionValue: "",
        pageCount: 0,
        pageNo: 0,
        allData: null,
        //修改
        updateData: false,
        //上传
        uploadData: false,
        formUpdate: {  //修改里面的
            id: "",
            dataNameCh: "",
            dateFormatTime: null,
            dataValue: "",
        },
        myChart:null,
        locals18n:null,
        fullscreenLoading: false,
    },
    created: function () {

        //取消loading
        try {
            window.parent.toggleLoading();
        } catch (e) {
        }
        // this.loadChart();
        // this.loadSiteType();
        this.getSiteValue();
        this.locals18n=this.common.Initial();
        this.$i18n.locale = this.locals18n;
        // this.siteTypeOptionValue=;
    }, watch: {
        'siteTypeOptionValue': {
            handler(newValue, oldValue) {
        console.log(this.siteTypeOptionValue)
                this.loadDate();
            }
        }, 'dateValue': {
            handler(newValue, oldValue) {
                if (this.siteTypeOptionValue != null && this.siteTypeOptionValue != "") {
                    this.loadDate();
                }
            }
        }
    }, methods: {
        submitUpdata(){
            //提交修改
            this.updateData = false;
            // console.log(this.formUpdate);
            // var reusltData = JSON.parse(JSON.stringify(this.formUpdate));
            // console.log(reusltData);
            // delete reusltData.dataTime;
            // console.log(this.formUpdate);
            this.$http.get(('../hardware/updataHardWare.do?id='+this.formUpdate.id+"&value="+this.formUpdate.dataRemark), {emulateJSON: true}).then(response => {
                // console.log(response.body);
                this.$message({
                    message: 'success！',
                    type: 'success'
                });
                //进行刷新
                this.loadDate();

            }, response => {
                this.$message.error('失败！');
            }).catch();
        },
        handleEdit(index, row) {
            //修改的弹窗
            this.updateData = true;
            row.dateFormatTime=new Date(parseInt(row.dataTime)).toLocaleString().replace(/:\d{1,2}$/, ' ');
            this.formUpdate=row;

        },
        upload(){
            //上传tab显示
            this.uploadtab = true;
        },
        uploadSuccess(res){
            console.log(res);
            this.uploadfilename = res.path;
        },
        getSiteValue(){
            this.$http.get('/readMap/getsitevalueinsql.do',{emulateJSON:true}).then(function (res){
                this.siteValue = res.data;
                //曲线图默认选择第一个站点
                this.selectedsiteValue = [this.siteValue[0].id];
                // this.echarttype = this.echarttypeoptions[0].value;
            })
        },
        submitUpload(){
            this.uploadtab = false
            var siteid = this.siteOptionValue.id;
            var filename = this.uploadfilename;
            this.$http.get('/hardware/updateharewaresite.do?siteid='+siteid+'&filename='+filename,{emulateJSON: true}).then(function (res) {
                // console.log(response.body);
                this.$message({
                    message: 'success！',
                    type: 'success'
                });
                //进行刷新
                this.loadDate();

            }, response => {
                this.$message.error('失败！');
            }).catch();
        },
        handleDelete(index, row) {
            console.log(index, row);
        },
        loadDate(t) {
            var beginDate = null;
            var endDate = null;
            if (this.dateValue != null) {
                beginDate = this.dateToString(this.dateValue[0]);
                endDate = this.dateToString(this.dateValue[1]);
            }
            var site = this.selectedsiteValue;
            if (this.echarttype === 'yancheng' && t === 1) {
                var siteids = [];
                for (let i = 0; i < this.siteValue.length; i++) {
                    siteids.push(this.siteValue[i].id);
                }
                this.selectedsiteValue = siteids;
            }
            this.$http.get('/hardware/getHarewaredData.do?languageType='+this.locals18n+'&site='+site+'&pre=' + this.siteTypeOptionValue.proNamech + "&begDate=" + beginDate + "&endDate=" + endDate, {emulateJSON: true}).then(function (res) {
                this.fullscreenLoading = true;
                this.pageCount = res.data.length;
                this.allData = res.data;
                if (res.data.length == 0) {
                    this.fullscreenLoading = false;
                    this.$message({
                        message: 'error',
                        type: 'error'
                    });
                } else {
                    this.fullscreenLoading = false;
                    // console.log(res.data);
                    // //统计图数据
                    var datasChart = this.fromatTimeData(res.data);
                    var duibidatas= this.fromatTimeDataUploadValue1(res.data);
                    var yanchengdatas= this.fromatTimeDatayancheng(res.data);
                    // var datasChartUpload= this.fromatTimeDataUploadValue(res.data);
                    var beginDate = this.dateToString(datasChart[0][0]);
                    var endDate = this.dateToString(datasChart[datasChart.length - 1][0]);
                    // this.myChart.clear();
                    var danweiy = res.data[0].danweiy;
                    var optiondata = this.dateToMap(res.data);
                    //alert(danweiy);
                    this.loadChart(datasChart, beginDate, endDate,duibidatas,yanchengdatas,danweiy,optiondata);
                    // //表格数据
                    // console.log(this.fromatTableData(res.data,1));
                    // console.log(res.data);
                    this.tableData = this.fromatTableData(res.data, 1);

                }

            });
        },

        loadChart(datas, beginDate, endDate,duibidatas,yanchengdatas,danweiy,optiondata) {

            var echarttype = this.echarttype;
             var texts = "";
            var optionValue=this.siteTypeOptionValue.proNamech;
            if("en"==this.locals18n){
                optionValue=this.siteTypeOptionValue.proNameen;
            }
            if (beginDate != null) {
                texts = beginDate + ' - ' + endDate + '      '+ optionValue ;
            }
            var xaxis,yaxis = null;var seriesdata = [];
            var linecolor={'库心':'#e42222'}
            if (echarttype === 'duibi'){
                xaxis = {
                    name:'时间',
                    type: 'time',
                    splitLine: {
                        lineStyle: {
                            type: 'dashed'
                        }
                    },
                    splitNumber: 20
                };
                yaxis = {
                    type: 'value',
                };
                for (let key in duibidatas) {
                    if (duibidatas.hasOwnProperty(key)) {
                        seriesdata.push({
                            name: key,
                            type: 'line',
                            data: duibidatas[key],
                            // color: linecolor[key]
                        })
                    }
                }
/*
                for (let i = 0; i < upDataData[data].length; i++) {
                    var d = upDataData[data][i];
                    seriesdata.push({
                        name: name,
                        type: 'line',
                        data: d
                    })
                }
*/
            }else {
                xaxis = {
                    name:'站点',
                    type: 'category',
                    splitLine: {
                        lineStyle: {
                            type: 'dashed'
                        }
                    },
                    splitNumber: 20
                };
                yaxis = {
                    name: '时间',
                    type: 'value',
                };
                var times = Object.keys(yanchengdatas);
                var bigtime = times[0];
                for (let i = 0; i < times.length; i++) {
                    if (times[i]>bigtime)
                        bigtime = times[i];
                }
                // for (let key in yanchengdatas) {
                //     if (yanchengdatas.hasOwnProperty(key)) {
                        seriesdata.push({
                            name: bigtime,
                            type: 'line',
                            data: yanchengdatas[bigtime],
                        })
                    // }
                // }
            }
            this.myChart = echarts.init(document.getElementById('eChartsDiv'));
            this.myChart.clear();
            option = {
                title: {
                    text: texts,
                    left: 'center'
                },
                /*legend: {
                    data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
                },*/
                tooltip: {
                    trigger: 'axis',
                   
                    axisPointer: {
                        type: 'cross'
                    }
                },
                xAxis: xaxis,
                yAxis: yaxis,
                series: seriesdata/*[
                   /!* {
                        name: optionValue,
                        type: 'line',
                        data: upDataData,
                        color: ["#39CCCC"],
                    },*!/{
                        name: optionValue,
                        type: 'line',
                        data: datas,
                        color: ["#e42222"],
                    },
                ]*/
            };

            var biaozunJson = "";
/*
            if (this.siteTypeOptionValue.proRemark.indexOf("NH3_ppm")!=-1) {
                // console.log("?");
                option.series.push(this.biaozunFormate(0.06, 'MAX', datas));
            } else if (this.siteTypeOptionValue.proRemark.indexOf("TN_ppm")!=-1) {
                // console.log("?1");
                option.series.push(this.biaozunFormate(1, 'MAX', datas));
            } else if (this.siteTypeOptionValue .proRemark.indexOf("TP_ppm")!=-1) {
                // console.log("?2");
                option.series.push(this.biaozunFormate(0.06, 'MAX', datas));
            }
*/
            this.myChart.setOption(option);
        },
        dateToMap(data){
            var result = {};
            for (let index = 0; index< data.length; index++){
                var d = data[index];
                if (d.dataNameCh in result){
                    result[d.dataNameCh].value.push(d.dataValue);
                    result[d.dataNameCh].data.push([new Date(d.dataTime),d.dataValue]);
                } else {
                    result[d.dataNameCh] = {
                        "danweiy": d.danweiy,
                        "dataTime": new Date(d.dataTime),
                        "value":[d.dataValue],
                        "data":[[new Date(d.dataTime),d.dataValue]]
                    }
                }
            }
            return result;
        },
        loadSiteType() {
            // this.$http.get('/hardware/getOptionHarword.do?type=SZWUchem_uchem.dat', {emulateJSON: true}).then(function (res) {
            //     this.siteTypeOption = res.data.data;
            //     this.siteTypeOptionValue = this.siteTypeOption[0];
            // });
        }, handleCurrentChange(val) {
            //分页的跳转
            this.tableData = this.fromatTableData(this.allData, val);
        }, fromatTimeData(date) {
            var data = [];
            for (let i = 0; i < date.length; i++) {
                var now = new Date(date[i].dataTime);
                var value = date[i].dataValue;
                var jjson = [now, value];
                data.push(jjson);
            }
            return data;
        },fromatTimeDataUploadValue(date) {
            var data = [];
            for (let i = 0; i < date.length; i++) {
                var now = new Date(date[i].dataTime);
                var value = "";
                if(date[i].dataRemark==""||date[i].dataRemark==null){
                    value = date[i].dataValue;
                }else{
                    value = date[i].dataRemark;
                }
                var jjson = [now, value];
                data.push(jjson);
            }
            return data;
        },fromatTimeDataUploadValue1(date) {
            //处理站点信息｛id:name｝
            var sitemap = {};
            for (let i = 0; i < this.siteValue.length; i++) {
                var site = this.siteValue[i];
                sitemap[site.id] = site.siteName;

            }
            var result = {};var arr = [];
            for (let i = 0; i < date.length; i++) {
                var v = date[i].dataValue;
                var now = new Date(date[i].dataTime);
                var value = "";
                if(isNumber(v)) {
                    value = v;
                }
                var jjson = [now, value];
                // arr.push(jjson);
                //{库心：[1.4，1.5]}
                var keysite = sitemap[date[i].dataRemark];
                if (keysite in result){
                    result[keysite].push(jjson);
                }else {
                    result[keysite] = [jjson]
                }
            }
            // result['site'] = sitemap[date[i].dataRemark];
            // result['data'] = arr;
            return result;
            function isNumber(value) {
                return /^-?\d+(\.\d+)?$/.test(value);
            }
        },fromatTimeDatayancheng(date) {
            //处理站点信息｛id:name｝
            var sitemap = {};
            for (let i = 0; i < this.siteValue.length; i++) {
                var site = this.siteValue[i];
                sitemap[site.id] = site.siteName;

            }
            var result = {};var arr = [];
            for (let i = 0; i < date.length; i++) {
                var v = date[i].dataValue;
                var keytime = new Date(date[i].dataTime);
                var value = "";
                if(isNumber(v)) {
                    value = v;
                }
                var site = sitemap[date[i].dataRemark];
                var jjson = [site, value];
                // arr.push(jjson);
                //{2024-9-1：[[库心，1.4]]}
                if (keytime in result){
                    result[keytime].push(jjson);
                }else {
                    result[keytime] = [jjson]
                }
            }
            return result;
            function isNumber(value) {
                return /^-?\d+(\.\d+)?$/.test(value);
            }
        }, fromatTableData(data, pageNo) {
            var beginNo = (pageNo - 1) * 10;
            var datas = [];
            var lengt = 0;
            if (beginNo + 10 > data.length) {
                lengt = data.length;
            } else {
                lengt = beginNo + 10
            }
            for (let i = beginNo; i < lengt; i++) {
                datas.push(data[i]);
            }
            return datas;
        }, dateToString(date) {
            // 将data转化为String
            var year = date.getFullYear();
            var month = (date.getMonth() + 1).toString();
            var day = (date.getDate()).toString();
            if (month.length == 1) {
                month = "0" + month;
            }
            if (day.length == 1) {
                day = "0" + day;
            }
            var dateTime = year + "-" + month + "-" + day;
            return dateTime;
        }, dateFormatTime(row, column) {
            //表格上面格式化时间
            var date = row[column.property];
            if (date == null) {
                return "";
            }
            return new Date(parseInt(date)).toLocaleString().replace(/:\d{1,2}$/, ' ');
        }, biaozunFormate(valueArray, TilteArray, datas1) {
            //标准线的生成
            var reuslt = {
                name: TilteArray,
                type: 'line',
                data: [[datas1[0][0], valueArray]],
                markLine: {
                    data: [{
                        name: TilteArray + '（' + valueArray + ")",
                        yAxis: valueArray,
                        label: {
                            formatter: '{b}',
                            show: true
                        }
                    }],
                }, color: ['#E6A23C'],
            };
            return reuslt;
        }
    }
});






